package com.snail.cloud.model;

import com.snail.cloud.model.vo.IotUserVo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author MR.LHL
 * @date 2024/2/26
 */
public class UserDetailsImpl implements UserDetails {

    public UserDetailsImpl(IotUser iotUser) {
        this.iotUser = iotUser;
    }

    private IotUser iotUser;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        Collection<GrantedAuthority> authorities = new ArrayList<>();
        ((IotUserVo) iotUser).getRoleList().forEach(role -> {
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        });
        return authorities;
    }

    @Override
    public String getPassword() {
        return iotUser.getPassword();
    }

    @Override
    public String getUsername() {
        return iotUser.getUsername();
    }

    public IotUser getIotUser() {
        return iotUser;
    }

    public void setIotUser(IotUser iotUser) {
        this.iotUser = iotUser;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return iotUser.getLocked() == null || !iotUser.getLocked();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
